package com.hb.trade.api.routine.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.Attachment;
import com.hb.core.pojo.Attachments;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import org.apache.poi.ss.usermodel.CellCopyPolicy;
import org.apache.poi.xssf.usermodel.*;
import org.fzzn.core.file.entity.HbFileInfo;
import org.fzzn.core.file.entity.UploadEntity;
import org.fzzn.core.file.service.PersistenceService;
import org.fzzn.core.model.constant.CommonConstant;
import org.fzzn.core.util.IdUtil;
import org.fzzn.core.web.util.SpringUtils;

import java.io.*;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 报名信息导出
 * @author tian
 * @date 2022年06月20日 17:27
 */
public class SignUpUtil {

    /**
     * 导出报名信息
     *
     * @author tian
     * @date 2022-6-20 15:57
     */
    public static Attachment expertSingUpExcel(List<JSONObject> signList, BidSectionDTO bidSectionDTO) throws IOException {
        Attachment attachment = new Attachment();
        InputStream in = null;
        ByteArrayOutputStream bos = null;
        try {
            PersistenceService persistenceService = SpringUtil.getBean(PersistenceService.class);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

            InputStream stream = SpringUtils.getLocalStream("template/signUp.xlsx");
            XSSFWorkbook templateWorkbook = new XSSFWorkbook(stream);
            XSSFSheet sheetAt = templateWorkbook.getSheetAt(0);
            //生成表头
            XSSFRow row = sheetAt.getRow(1);
            row.getCell(1).setCellValue(bidSectionDTO.getProjectName());
            row.getCell(5).setCellValue(bidSectionDTO.getProjectCode());

            row = sheetAt.getRow(2);
            row.getCell(1).setCellValue(bidSectionDTO.getSectionName());
            row.getCell(5).setCellValue(bidSectionDTO.getSectionCode());

            row = sheetAt.getRow(3);
            // 报名开始时间
            if (null != bidSectionDTO.getSignStartTime()) {
                row.getCell(1).setCellValue(bidSectionDTO.getSignStartTime().format(formatter));
            }
            // 报名截至时间
            if (null != bidSectionDTO.getSignEndTime()) {
                row.getCell(5).setCellValue(bidSectionDTO.getSignEndTime().format(formatter));
            }


            // 报名信息信息从第6行开始创建
            int rowNum = 6;
            // 组建专家信息
            for (int i = 0; i < signList.size(); i++) {
                JSONObject signInfo = signList.get(i);
                sheetAt.copyRows(5, 5, rowNum + i, new CellCopyPolicy());

                row = sheetAt.getRow(rowNum + i);
                row.getCell(0).setCellValue((String) signInfo.get("orgName"));
                row.getCell(2).setCellValue((String) signInfo.get("contactName"));
                row.getCell(3).setCellValue((String) signInfo.get("mobile"));
                row.getCell(4).setCellValue((String) signInfo.get("signUpTime"));
                row.getCell(5).setCellValue((String) signInfo.get("signupStatus"));
            }

            bos = new ByteArrayOutputStream();
            templateWorkbook.write(bos);
            byte[] brray = bos.toByteArray();
            in = new ByteArrayInputStream(brray);

            // 上传
            File scheduleExcel = FileUtil.writeBytes(brray, new File(CommonConstant.TEMP_PATH + "/" + bidSectionDTO.getSectionCode() + "/" + "报名记录" + ".xlsx"));
            UploadEntity uploadEntity = new UploadEntity(scheduleExcel);
            List<HbFileInfo> hbFileInfos = persistenceService.uploadServer(uploadEntity);
            if (CollUtil.isNotEmpty(hbFileInfos)) {
                // 保存入库
                Attachments attachments = new Attachments();
                attachment = JSONUtil.toBean(JSONUtil.parseObj(hbFileInfos.get(0)), Attachment.class);
                attachment.setId(IdUtil.getId());
                attachments.add(attachment);

                return attachment;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                in.close();
            }
            if (null != bos) {
                bos.close();
            }
        }
        return attachment;
    }
}
